import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import br.com.news.utils.beans.BaseBean;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;



public class Generic<T extends BaseBean> {

	@SuppressWarnings("unchecked")
	public List<T> listFromJson(String url,BaseBean base) {
		URLConnection urlConnection;
		url = Constante.contextoRest+url;


		try {
			urlConnection = new URL(url).openConnection();
			urlConnection.connect();
			JsonReader reader = new JsonReader(new InputStreamReader(urlConnection.getInputStream()));

			JsonParser parser = new JsonParser();
			JsonElement rootElement = parser.parse(reader);
			JsonArray tweetsJson = rootElement.getAsJsonArray();

			List<T> timeline = new ArrayList<T>();
			Gson myGson = new Gson();
			for ( JsonElement tweetElement : tweetsJson){
				BaseBean myTweet = myGson.fromJson(tweetElement,base.getClass());
				timeline.add((T) myTweet);
				System.out.println(myTweet);
			}

			return timeline;
		} catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;


	}

}
